package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
<<<<<<< HEAD
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
=======
>>>>>>> adc48831cde55357ef0420c5deac2882fa85ad7a
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:03
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping(value = "/findAll")
    public Result findAll() {
        try {
            List<Menu> MenuList = menuService.findAll();
            return new Result(true,"",MenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"");
        }
    }

<<<<<<< HEAD
    //分页查询菜单列表
    @RequestMapping( "findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = menuService.findPage(
                queryPageBean.getQueryString(),
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize()
        );
        return pageResult;
    }
    // 新增检查项（返回 public Result(boolean flag, String message)）
    @RequestMapping(value = "/add")
//    @PreAuthorize(value = "hasAuthority('MENU_ADD')")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            // 成功
            return new Result(true,"添加菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            // 成功
            return new Result(false, "添加菜单失败");
        }

    }
    // 根据id，删除检查项
    @RequestMapping(value = "/delete")
//    @PreAuthorize(value = "hasAuthority('MENU_DELETE123')")
    public Result delete(Integer id){
        try {
            menuService.deleteById(id);
            return new Result(true,"删除菜单成功");
        }catch (RuntimeException e){ // 遇到运行时异常，表示检查项和检查组的关联表中存在数据
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除菜单失败");
        }
    }

    // 使用id查询检查项
    @RequestMapping(value = "/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,"回显菜单成功",menu);
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"回显菜单失败");
        }
    }

    // 使用id查询检查项
    @RequestMapping(value = "/edit")
//    @PreAuthorize(value = "hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,"修改菜单成功");
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改菜单失败");
        }
    }
=======

>>>>>>> adc48831cde55357ef0420c5deac2882fa85ad7a
}
